package com.nlelpct.manage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nlelpct.manage.constant.SysAuthenticationConstant;
import com.nlelpct.manage.constant.SysManageOperateTypeConstant;
import com.nlelpct.manage.model.entity.SysEmployee;
import com.nlelpct.manage.model.query.TerminalInfo;
import com.nlelpct.manage.model.vo.EsAuthenticationLog;
import com.nlelpct.manage.model.vo.EsManageSystemLog;
import com.nlelpct.manage.service.ILogParamInfoService;
import com.nlelpct.manage.service.LogBaseService;
import com.nlelpct.manage.utils.GetLoginInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
//@SpringBootTest
public class ILogParamInfoServiceImpl implements ILogParamInfoService {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${kafkaTopic.EsSystemManageOperateKafka}")
    private String systemTopic;

    @Value("${kafkaTopic.EsAuthenticationKafka}")
    private String authenticationTopic;

    @Autowired
    private LogBaseService logBaseService;

    /**
     * 发送系统操作日志到kafka
     *
     * @param systemLog 系统操作日志对象
     */
    @Override
   public void sendSystemLog(EsManageSystemLog systemLog){

        TerminalInfo terminalInfo = logBaseService.getTerminalInfo();
        SysEmployee employee=logBaseService.getEmployee();
        CompletableFuture.supplyAsync(() -> {
            try {
                // 拼接基础数据
                if (ObjectUtil.isEmpty(systemLog.getId())) {
                    systemLog.setId(logBaseService.getSysTemLogId());
                }
                systemLog.setOperator(employee.getEmployeeName());
                systemLog.setOperatorId(employee.getEmployeeId());
                systemLog.setOperatorNumber(employee.getEmployeeNum());
                if (ObjectUtil.isEmpty(systemLog.getOperatingTime())) {
                    systemLog.setOperatingTime(System.currentTimeMillis());
                }
                systemLog.setCompanyId(employee.getCompanyId());
                systemLog.setOperatingSystem(terminalInfo.getOs());
                systemLog.setIpAddress(terminalInfo.getIpAddress());
                systemLog.setArea(terminalInfo.getIpArea());
                systemLog.setBrowser(terminalInfo.getBrowser());
                systemLog.setPage(Optional.ofNullable(logBaseService.getPage()).orElse(""));
                systemLog.setModule(Optional.ofNullable(logBaseService.getModule()).orElse(""));
                kafkaTemplate.send(systemTopic, JSON.toJSONString(systemLog));
                log.info("成功向kafka发送一条系统日志,日志信息:{}", systemLog);
            } catch (Exception e) {
                log.error("向kafka发送系统日志失败,日志ID:{}, 具体原因如下:", systemLog.getId());
                e.printStackTrace();
            }
            return null;
        });

    }

    /**
     * 发送认证日志到kafka
     *
     * @param
     */
    @Override
   public void sendAuthenticationLog(EsAuthenticationLog esAuthenticationLog){
        TerminalInfo terminalInfo = logBaseService.getTerminalInfo();
        SysEmployee employee=logBaseService.getEmployee();
        CompletableFuture.supplyAsync(() -> {
            try {
                // 拼接基础数据
                if (ObjectUtil.isEmpty(esAuthenticationLog.getId())) {
                    esAuthenticationLog.setId(logBaseService.getSysTemLogId());
                }
                esAuthenticationLog.setOperatorId(employee.getEmployeeId());
                //esAuthenticationLog.setOperatorNumber(employee.getEmployeeNum());
                if (ObjectUtil.isEmpty(esAuthenticationLog.getOperateTimeLong())) {
                    esAuthenticationLog.setOperateTimeLong(System.currentTimeMillis());
                }
                esAuthenticationLog.setCompanyId(employee.getCompanyId());
                esAuthenticationLog.setSystemId(GetLoginInfoUtils.getSystemId());
                Map<String ,Object> centenmap=new HashMap<>();
                centenmap.put("ip","192.168.11.11");
                centenmap.put("area","中国");
                centenmap.put("Browser","192.168.11.11");
                esAuthenticationLog.setContent(JSONObject.toJSONString(centenmap));
                //esAuthenticationLog.setIpAddress(terminalInfo.getIp_address());
               // esAuthenticationLog.setArea(terminalInfo.getIp_area());
                //esAuthenticationLog.setBrowser(terminalInfo.getB_kernel());
               // esAuthenticationLog.setPage(Optional.ofNullable(logBaseService.getPage()).orElse(""));
               // esAuthenticationLog.setModule(Optional.ofNullable(logBaseService.getModule()).orElse(""));
                kafkaTemplate.send(authenticationTopic, JSON.toJSONString(esAuthenticationLog));
                log.info("成功向kafka发送一条系统日志,日志信息:{}", esAuthenticationLog);
            } catch (Exception e) {
                log.error("向kafka发送系统日志失败,日志ID:{}, 具体原因如下:", esAuthenticationLog.getId());
                e.printStackTrace();
            }
            return null;
        });
    }


   // @Test
    public void inertlogdemo(){
        EsManageSystemLog systemLog=new EsManageSystemLog();
        //TerminalInfo terminalInfo = logBaseService.getTerminalInfo();
       // SysEmployee employee=logBaseService.getEmployee();
       // CompletableFuture.supplyAsync(() -> {
            try {
                // 拼接基础数据
               // systemLog.setPage("page");

                systemLog.setBusinessModule(1);
                systemLog.setOperand("wu");
                systemLog.setOperationIntroduction("no message");

                systemLog.setOperationType(SysManageOperateTypeConstant.ADD);
                systemLog.setSystemId("sys");
                if (ObjectUtil.isEmpty(systemLog.getId())) {
                    systemLog.setId("424353535450000000534500");
                }
                systemLog.setOperator("lianhuamin");
                systemLog.setOperatorId("33354353535");
                systemLog.setOperatorNumber("gd_007");
                if (ObjectUtil.isEmpty(systemLog.getOperatingTime())) {
                    systemLog.setOperatingTime(System.currentTimeMillis());
                }
                systemLog.setCompanyId("7de3d791be05460697d5dbbd39eccd81");
                systemLog.setOperatingSystem("window");
                systemLog.setIpAddress("192.168.11.11");
                systemLog.setArea("192.168.11.12");
                systemLog.setBrowser("test");
                systemLog.setPage(Optional.ofNullable("system").orElse(""));
                systemLog.setModule(Optional.ofNullable("add").orElse(""));
                kafkaTemplate.send(systemTopic, JSON.toJSONString(systemLog));
                log.info("成功向kafka发送一条系统日志,日志信息:{}", systemLog);
            } catch (Exception e) {
                log.error("向kafka发送系统日志失败,日志ID:{}, 具体原因如下:", systemLog.getId());
                e.printStackTrace();
            }

    }

   // @Test
    public void insertAuthenlog(){
       // TerminalInfo terminalInfo = logBaseService.getTerminalInfo();
       // SysEmployee employee=logBaseService.getEmployee();
       // CompletableFuture.supplyAsync(() -> {
        EsAuthenticationLog esAuthenticationLog=new EsAuthenticationLog();
            try {

                esAuthenticationLog.setId("4254235353");
                esAuthenticationLog.setSystemId("sys");
                esAuthenticationLog.setAuthenticationTypeId(200);
                esAuthenticationLog.setAuthenticationWay("way");
               // esAuthenticationLog.setOperationIntroduction("no message");
                // 拼接基础数据
                if (ObjectUtil.isEmpty(esAuthenticationLog.getId())) {
                    esAuthenticationLog.setId("3333333333333333333333333");
                }
                esAuthenticationLog.setOperatorId("lianhuamin");
               // esAuthenticationLog.setOperatorNumber("gd007");
                if (ObjectUtil.isEmpty(esAuthenticationLog.getOperateTimeLong())) {
                    esAuthenticationLog.setOperateTimeLong(System.currentTimeMillis());
                }
                esAuthenticationLog.setCompanyId("7de3d791be05460697d5dbbd39eccd81");
                //esAuthenticationLog.setOperatingSystem("os");
                Map<String ,Object> centenmap=new HashMap<>();
                centenmap.put("ip","192.168.11.11");
                centenmap.put("area","中国");
                centenmap.put("Browser","192.168.11.11");
                esAuthenticationLog.setContent(JSONObject.toJSONString(centenmap));
                kafkaTemplate.send(authenticationTopic, JSON.toJSONString(esAuthenticationLog));
                log.info("成功向kafka发送一条系统日志,日志信息:{}", esAuthenticationLog);
            } catch (Exception e) {
                log.error("向kafka发送系统日志失败,日志ID:{}, 具体原因如下:", esAuthenticationLog.getId());
                e.printStackTrace();
            }
    }
}
